<!--  -->
<template>
  <div style="display: flex; justify-content: space-between">
    <a-form size="small" :model="form" layout="inline">
      <CorporateSectorCascader
        ref="cascaderRef"
        :configCascader="configCascader"
        @changeCascader="changeCascader"
      />
      <a-form-item label="日期" label-col-flex="80px">
        <a-range-picker
          popup-container="html"
          class="bacClass"
          @select="onSelect"
          style="width: 300px"
          @clear="onClear"
        />
      </a-form-item>
    </a-form>
    <Toolbar
      :items="tabTtpe == 1 ? ['查询', '新增', '删除', '审核'] : ['查询']"
      @select="cx"
      @add="adds"
      @del="delSles"
      @audit="auditss"
    >
    </Toolbar>
  </div>
  <div class="gang"></div>
  <a-tabs :lazy-load="true" type="card" @change="tabsChange" justify>
    <a-tab-pane title="未审核" :key="1">
      <div class="cssTable" style="height: 100%">
        <a-spin
          :loading="form.pageLoading"
          tip="加载中"
          dot
          style="height: 100%; width: 100%; z-index: 999"
        >
          <AgGridVuesCopys
            :rowHeight="33"
            :headerHeight="33"
            :suppressCopyRowsToClipboard="true"
            :suppressDragLeaveHidesColumns="true"
            rowSelection="multiple"
            @grid-ready="gridReady"
            style="height: calc(100% - 100px)"
            :defaultColDef="defaultColDef"
            class="ag-theme-alpine"
            :rowData="dataList"
            :columnDefs="tc"
            :localeText="localeText1"
          ></AgGridVuesCopys>
          <paginationCopys
            :page-size-options="pagingList1"
            :total="total"
            show-total
            show-jumper
            show-page-size
            @change="PageSize"
            @page-size-change="change1"
            style="justify-content: center"
            :page-size="size"
            :current="page"
          />
        </a-spin>
      </div>
    </a-tab-pane>
    <a-tab-pane title="已审核" :key="2">
      <div class="cssTable" style="height: 100%">
        <a-spin
          :loading="form.pageLoading"
          tip="加载中"
          dot
          style="height: 100%; width: 100%; z-index: 999"
        >
          <AgGridVuesCopys
            :rowHeight="33"
            :headerHeight="33"
            :suppressCopyRowsToClipboard="true"
            :suppressDragLeaveHidesColumns="true"
            rowSelection="multiple"
            @grid-ready="gridReady2"
            style="height: calc(100% - 100px)"
            :defaultColDef="defaultColDef"
            class="ag-theme-alpine"
            :rowData="dataList"
            :columnDefs="tc"
            :localeText="localeText1"
          ></AgGridVuesCopys>
          <paginationCopys
            :page-size-options="pagingList1"
            :total="total2"
            show-total
            show-jumper
            show-page-size
            @change="PageSize2"
            @page-size-change="change12"
            style="justify-content: center"
            :page-size="size2"
            :current="page2"
          />
        </a-spin>
      </div>
    </a-tab-pane>
  </a-tabs>
  <a-drawer
    style="z-index: 999"
    :drawer-style="{ zIndex: 9999 }"
    :mask-closable="false"
    :visible="pageData.showAdd"
    unmountOnClose
    placement="bottom"
    :closable="false"
    popup-container=".arco-tabs-content"
    height="550"
    v-loading="loadings"
  >
    <template #title>
      {{ pageData.sideTitle }}
    </template>
    <template #footer>
      <a-button @click="pageData.showAdd = false">关闭</a-button>
      <a-button
        :loading="loadings"
        @click="sideOk"
        type="primary"
        v-if="pageData.sideTitle != '生产补贴单明细'"
        >保存</a-button
      >
    </template>
    <div>
      <a-form
        ref="formRef"
        layout="horizontal"
        :model="dataForm"
        size="mini"
        v-if="pageData.sideTitle != '生产补贴单明细'"
      >
        <a-row :gutter="24">
          <a-col :span="6">
            <a-form-item label-col-flex="100px" label="补贴单号" field="subsidyCode">
              <a-input
                class="bacClass"
                placeholder="自动生成"
                v-model="dataForm.subsidyCode"
                disabled
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item
              label-col-flex="100px"
              label="补贴日期"
              field="subsidyDate"
              :rules="[{ required: true, message: '补贴日期必填' }]"
              :validate-trigger="['change', 'blur']"
            >
              <a-date-picker
                popup-container="html"
                class="bacClass"
                v-model="dataForm.subsidyDate"
                style="width: 300px"
                :disabled="pageData.sideTitle == '生产补贴单明细'"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item
              label-col-flex="100px"
              label="公司"
              field="companyName"
              :rules="[{ required: true, message: '公司必填' }]"
              :validate-trigger="['change', 'blur']"
            >
              <a-input class="bacClass" placeholder="" v-model="dataForm.companyName" disabled />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label-col-flex="100px" label="部门" field="departmentId">
              <a-cascader
                ref="cascaders"
                v-model="dataForm.departmentId"
                class="bacClass"
                :options="options"
                :style="{ width: '200px' }"
                placeholder=""
                @click="bmClick"
                allow-clear
                :disabled="pageData.sideTitle == '生产补贴单明细'"
                popup-container="html"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="6">
            <a-form-item
              label-col-flex="100px"
              label="补贴类型"
              field="subsidyType"
              :rules="[{ required: true, message: '补贴类型必填' }]"
              :validate-trigger="['change', 'blur']"
            >
              <a-select
                popup-container="html"
                class="bacClass"
                allow-clear
                v-model="dataForm.subsidyType"
                @click="ljlxClick"
                :disabled="pageData.sideTitle == '生产补贴单明细'"
                @change="ljlxChange"
              >
                <a-option v-for="item in ljlxList" :value="item.subsidyType">{{
                  item.subsidyType
                }}</a-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item
              label-col-flex="100px"
              label="补贴类型金额"
              field="subsidyTypeAmount"
              :rules="[{ required: true, message: '补贴类型金额必填' }]"
              :validate-trigger="['change', 'blur']"
            >
              <a-input-number
                class="bacClass"
                placeholder=""
                v-model="dataForm.subsidyTypeAmount"
                :disabled="true"
                :precision="2"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label-col-flex="100px" label="备注" field="remark">
              <a-input
                class="bacClass"
                placeholder=""
                v-model="dataForm.remark"
                :disabled="pageData.sideTitle == '生产补贴单明细'"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-space>
      <a-button
        type="primary"
        size="mini"
        @click="xzClicks"
        v-if="pageData.sideTitle != '生产补贴单明细'"
        >新增</a-button
      >
      <a-button
        type="primary"
        size="mini"
        @click="mxDelet"
        v-if="pageData.sideTitle != '生产补贴单明细'"
        >删除</a-button
      >
    </a-space>
    <!-- <a-table row-key="userId" :columns="columnsTable" :data="dataListmx"
      :row-selection="{ type: 'checkbox', showCheckedAll: true }" v-model:selectedKeys="selectedKeys" :pagination="false"
      size="small" :bordered="{ cell: true }" v-if="pageData.sideTitle != '生产补贴单明细'">
      <template #planOrderCode="{ record }">
        <a-select popup-container="html" class="bacClass" allow-clear v-model="record.planOrderCode"
          @click="planOrderCodeClick" :disabled="pageData.sideTitle == '生产补贴单明细'" style="width: 170px" allow-search>
          <a-option v-for="item in planOrderCodelist" :value="item.planOrderCode">{{
            item.planOrderCode
          }}</a-option>
        </a-select>
      </template>
      <template #machineCode="{ record }">
        <a-select popup-container="html" class="bacClass" allow-clear v-model="record.machineCode"
          @click="machineCodeClick(record.planOrderCode)"
          :disabled="pageData.sideTitle == '生产补贴单明细' || !record.planOrderCode" style="width: 170px" allow-search>
          <a-option v-for="item in machineCodeList" :value="item.machineCode">{{
            item.machineCode
          }}</a-option>
        </a-select>
      </template>
      <template #subsidyAmount="{ record }">
        {{ (record.subsidyAmount = dataForm.subsidyTypeAmount) }}
      </template>
      <template #remark="{ record }">
        <a-input class="bacClass" v-model="record.remark" :disabled="pageData.sideTitle == '生产补贴单明细'" />
      </template>
    </a-table> -->
    <AgGridVuesCopys
      :rowHeight="33"
      :headerHeight="33"
      :suppressCopyRowsToClipboard="true"
      :suppressDragLeaveHidesColumns="true"
      rowSelection="multiple"
      @grid-ready="gridReadyAdds"
      style="height: 55vh"
      :defaultColDef="defaultColDef"
      class="ag-theme-alpine"
      :rowData="dataListmx"
      :columnDefs="tcAdds"
      :localeText="localeText1"
      v-if="pageData.sideTitle != '生产补贴单明细'"
    ></AgGridVuesCopys>
    <!-- <a-table row-key="userId" :columns="columnsTables" :data="dataListmx"
      :row-selection="{ type: 'checkbox', showCheckedAll: true }" v-model:selectedKeys="selectedKeys" :pagination="false"
      size="small" :bordered="{ cell: true }" v-else>
      <template #confirmState="{ record }">
        <a-tag color="green" v-if="record.confirmState == 0">未确认</a-tag>
        <a-tag color="#45a769" v-else>已确认</a-tag>
      </template>
    </a-table> -->
    <AgGridVuesCopys
      :rowHeight="33"
      :headerHeight="33"
      :suppressCopyRowsToClipboard="true"
      v-else
      :suppressDragLeaveHidesColumns="true"
      rowSelection="multiple"
      style="height: 55vh"
      :defaultColDef="defaultColDef"
      class="ag-theme-alpine"
      :rowData="dataListmx"
      :columnDefs="tcAddsMx"
      :localeText="localeText1"
    ></AgGridVuesCopys>
  </a-drawer>
  <a-modal v-model:visible="visible" @ok="handleOk" @cancel="visible = false" fullscreen>
    <template #title> 员工明细 </template>
    <div style="width: 100%; margin-bottom: 10px; text-align: right">
      <div> <a-button type="primary" size="small" @click="mxSelect">查询</a-button></div>
    </div>
    <!-- <a-table size="small" row-key="id" :columns="columnsTable2" :data="dataListmx2"
      :row-selection="{ type: 'checkbox', showCheckedAll: true }" v-model:selectedKeys="selectedKeys2" :pagination="false"
      :bordered="{ cell: true }">
    </a-table> -->
    <AgGridVuesCopys
      :rowHeight="33"
      :headerHeight="33"
      :suppressCopyRowsToClipboard="true"
      :suppressDragLeaveHidesColumns="true"
      rowSelection="multiple"
      style="height: 75vh"
      @grid-ready="gridReadyAddYg"
      :defaultColDef="defaultColDef"
      class="ag-theme-alpine"
      :rowData="dataListmx2"
      :columnDefs="tcAddYg"
      :localeText="localeText1"
    ></AgGridVuesCopys>
  </a-modal>
</template>

<script lang="ts" setup>
  import { reactive, toRefs, ref, onMounted } from 'vue'
  import Toolbar from '@/components/Toolbar.vue'
  import { post, get } from '@/api/http'
  import { Message, Modal } from '@arco-design/web-vue'
  
  import { localeText } from '@/api/aggridZw'
  import { pagingList } from '@/api/paging'
  import CorporateSectorCascader from '@/components/corporateSectorCascader/inde.vue'
  import { slicing } from '@/utils/caseFrimAndSecRig'
  import dayjs from 'dayjs'
  const pagingList1: any = ref(pagingList)
  const localeText1: any = ref(localeText)

  let a = [
    {
      showRowGroup: 'hide_def',
      headerName: '',
      checkboxSelection: true,
      headerCheckboxSelection: true,
      pinned: 'left',
      width: 50,
      resizable: false,
      filter: false,
      sortable: false,
      minWidth: 50,
      maxWidth: 50,
      lockPosition: 'left',
    },
  ]

  const tabTtpe = ref(1)

  const loadings = ref(false)

  const tabsChange = (e) => {
    if (e == 1) {
      doRefresh()
    } else {
      doRefresh2()
    }
    tabTtpe.value = e
  }

  const form: any = reactive({
    donateProject: '',
  })

  const defaultColDef: any = {
    sortable: true,
    resizable: true,
    filter: true,
  }
  const gridOption = ref()
  const colState = ref()
  const gridReady = (params: any) => {
    gridOption.value = params.api
    colState.value = params.columnApi
  }

  const gridOption2 = ref()
  const gridReady2 = (params: any) => {
    gridOption2.value = params.api
  }

  const gridOptionAdds = ref()
  const gridReadyAdds = (params: any) => {
    gridOptionAdds.value = params.api
  }
  const gridOptionAddYg = ref()
  const gridReadyAddYg = (params: any) => {
    gridOptionAddYg.value = params.api
  }

  const total = ref(0)
  const page = ref(1)
  const size = ref(10)
  function PageSize(val: any) {
    page.value = val
    doRefresh()
  }
  function change1(val: any) {
    size.value = val
    doRefresh()
  }

  const total2 = ref(0)
  const page2 = ref(1)
  const size2 = ref(10)
  function PageSize2(val: any) {
    page2.value = val
    doRefresh2()
  }
  function change12(val: any) {
    size2.value = val
    doRefresh2()
  }

  const dataList: any = ref([])

  function doRefresh() {
    form.pageLoading = true
    post({
      url: 'pieceData/ProduceSubsidy/getPage',
      data: {
        page: page.value,
        size: size.value,
        auditState: 0,
        beginDate: form.beginDate,
        endDate: form.endDate,
        companyId: form.companyId,
      },
    })
      .then(({ data, code }: any) => {
        if (code === 200) {
          dataList.value = data.list
          gridOption.value.setRowData(data.list)
          total.value = data.total
        }
      })
      .catch(console.log)
      .finally(() => {
        form.pageLoading = false
      })
  }

  function doRefresh2() {
    form.pageLoading = true
    post({
      url: 'pieceData/ProduceSubsidy/getPage',
      data: {
        page: page.value,
        size: size.value,
        auditState: 1,
        beginDate: form.beginDate,
        endDate: form.endDate,
        companyId: form.companyId,
      },
    })
      .then(({ data, code }: any) => {
        if (code === 200) {
          dataList.value = data.list
          gridOption.value.setRowData(data.list)
          total.value = data.total
        }
      })
      .catch(console.log)
      .finally(() => {
        form.pageLoading = false
      })
  }

  const cx = () => {
    if (tabTtpe.value == 1) {
      page.value = 1
      doRefresh()
    } else {
      page2.value = 1
      doRefresh2()
    }
  }

  onMounted(() => {
    pageData.rHeight = document.getElementsByClassName('main-section')[0]?.clientHeight
    //弹出层的高
    if (pageData.rHeight !== undefined) {
      pageData.rHeight = pageData.rHeight
    }
    doRefresh()
  })
  const pageData: any = reactive({
    showAdd: false,
    rHeight: 0,
    sideTitle: '新增',
  })

  const dataForm: any = reactive({
    companyId: '',
    companyName: '',
    departmentName: '',
    departmentId: '',
    subsidyCode: '',
    subsidyDate: '',
    subsidyType: '',
    subsidyTypeAmount: '',
    remark: '',
  })

  const tableColumns: any = [
    {
      title: '公司',
      key: 'companyName',
      dataIndex: 'companyName',
      width: 150,
    },
    {
      title: '部门名称',
      key: 'departmentName',
      dataIndex: 'departmentName',
      width: 150,
    },
    {
      title: '补贴单号',
      key: 'subsidyCode',
      dataIndex: 'subsidyCode',
      width: 150,
    },
    {
      title: '补贴日期',
      key: 'subsidyDate',
      dataIndex: 'subsidyDate',
      width: 150,
    },
    {
      title: '补贴类型',
      key: 'subsidyType',
      dataIndex: 'subsidyType',
      width: 150,
    },
    {
      title: '补贴类型金额',
      key: 'subsidyTypeAmount',
      dataIndex: 'subsidyTypeAmount',
      width: 150,
    },
    {
      title: '总金额',
      key: 'amount',
      dataIndex: 'amount',
      width: 150,
    },
    {
      title: '备注',
      key: 'remark',
      dataIndex: 'remark',
      width: 150,
    },
    {
      title: '创建人',
      key: 'createName',
      dataIndex: 'createName',
      width: 150,
    },
    {
      title: '创建时间',
      key: 'createTime',
      dataIndex: 'createTime',
      width: 150,
    },
    {
      title: '修改人',
      key: 'updateName',
      dataIndex: 'updateName',
      width: 150,
    },
    {
      title: '修改时间',
      key: 'updateTime',
      dataIndex: 'updateTime',
      width: 150,
    },
    {
      title: '审核状态',
      key: 'auditState',
      dataIndex: 'auditState',
      width: 150,
      fixed: 'right',
    },
    {
      title: '操作',
      key: 'alites',
      dataIndex: 'alites',
      slotName: 'alites',
      width: 80,
      fixed: 'right',
    },
  ]

  let tc: any[] = tableColumns.map((o: any) => {
    let obj = {
      headerName: o.title,
      field: o.key,
      pinned: o['fixed'],
    }
    if (o.key === 'alites') {
      obj['resizable'] = false
      obj['filter'] = false
      obj['sortable'] = false
      obj['lockPosition'] = 'right'
      obj['width'] = 100
      obj['cellRenderer'] = {
        template: `
              <a-space>
                <el-button v-if="taval == 1" style="color:#165dff;" link @click="a1" >修改</el-button>
                <el-button style="color:#165dff;" link @click="a2" >明细</el-button>
              </a-space>
            `,
        setup(props: any) {
          const a1 = () => {
            for (let item in props.params.data) {
              dataForm[item] = props.params.data[item]
            }
            post({
              url: 'pieceData/ProduceSubsidyDetail/getList',
              data: {
                produceSubsidyId: props.params.data.id,
              },
            }).then(({ data }) => {
              dataListmx.value = data
            })
            bmClick()
            pageData.showAdd = true
            pageData.sideTitle = '修改生产补贴单'
          }
          const a2 = () => {
            for (let item in props.params.data) {
              dataForm[item] = props.params.data[item]
            }
            post({
              url: 'pieceData/ProduceSubsidyDetail/getList',
              data: {
                produceSubsidyId: props.params.data.id,
              },
            }).then(({ data }) => {
              dataListmx.value = data
            })
            bmClick()
            pageData.showAdd = true
            pageData.sideTitle = '生产补贴单明细'
          }
          return {
            a1,
            a2,
            taval: tabTtpe.value,
          }
        },
      }
    }
    if (o.key === 'auditState') {
      obj['resizable'] = false
      obj['filter'] = true
      obj['filterParams'] = {
        valueFormatter: function (params: any) {
          return params.value == 0 ? '未审核' : params.value == 1 ? '已审核' : ''
        },
      }
      obj['sortable'] = false
      obj['lockPosition'] = 'right'
      obj['width'] = 150
      obj['cellRenderer'] = {
        template: `<a-tag color="#45a769" v-if="params.data.auditState==1">{{val}}</a-tag><a-tag color="green" v-else>{{val}}</a-tag>`,
        setup(props: any) {
          let val = ref()
          if (props.params.data.auditState == 0) {
            val.value = '未审核'
          } else {
            val.value = '已审核'
          }
          return {
            val,
          }
        },
      }
    }
    return obj
  })
  tc = a.concat(tc)

  const adds = () => {
    dataListmx.value = []
    if (dataForm.id) {
      delete dataForm.id
    }
    for (let item in dataForm) {
      dataForm[item] = ''
    }
    frimIDandName()
    pageData.showAdd = true
    pageData.sideTitle = '新增生产补贴单'
  }

  const formRef = ref()

  const sideOk = () => {
    formRef.value?.validate().then((err) => {
      if (!err) {
        if (pageData.sideTitle == '新增生产补贴单') {
          loadings.value = true
          post({
            url: 'pieceData/ProduceSubsidy/insertList',
            data: {
              ...dataForm,
              ProduceSubsidyDetail: dataListmx.value,
            },
          })
            .then(({ code, message }) => {
              if (code == 200) {
                Message.success('成功')
                pageData.showAdd = false
                doRefresh()
              } else {
                Message.error(message)
              }
            })
            .finally(() => {
              loadings.value = false
            })
        } else if (pageData.sideTitle == '修改生产补贴单') {
          loadings.value = true
          post({
            url: 'pieceData/ProduceSubsidy/updateList',
            data: {
              ...dataForm,
              ProduceSubsidyDetail: dataListmx.value,
            },
          })
            .then(({ code, message }) => {
              if (code == 200) {
                Message.success('成功')
                pageData.showAdd = false
                doRefresh()
              } else {
                Message.error(message)
              }
            })
            .finally(() => {
              loadings.value = false
            })
        } else {
          pageData.showAdd = false
        }
      }
    })
  }

  const delSles = () => {
    if (gridOption.value.getSelectedRows().length == 0) {
      Message.error('请勾选数据')
      return false
    }
    Modal.confirm({
      title: '系统提示',
      content: '确认删除选中的数据?',
      onOk: () => {
        post({
          url: 'pieceData/ProduceSubsidy/delete',
          data: gridOption.value.getSelectedRows().map((res: any) => res.id),
        }).then((res: any) => {
          if (res.code == 200) {
            Message.success('删除成功')
            doRefresh()
          } else Message.error(res.message)
        })
      },
    })
  }

  const auditss = () => {
    if (gridOption.value.getSelectedRows().length == 0) {
      Message.error('请勾选数据')
      return false
    }
    Modal.confirm({
      title: '系统提示',
      content: '确认审核选中的数据?',
      onOk: () => {
        post({
          url: 'pieceData/ProduceSubsidy/setAudit',
          data: gridOption.value.getSelectedRows().map((res: any) => {
            return {
              id: res.id,
              auditState: 1,
            }
          }),
        }).then((res: any) => {
          if (res.code == 200) {
            Message.success('审核成功')
            doRefresh()
          } else Message.error(res.message)
        })
      },
    })
  }

  const onSelect = (dateString: any) => {
    if (dateString[0]) {
      const d1 = new Date(dateString[0])
      const d2 = new Date(dateString[1])
      if (d1 > d2) {
        dateString.reverse()
      }
    }
    form.beginDate = dateString[0]
    form.endDate = dateString[1]
  }
  const onClear = () => {
    form.beginDate = ''
    form.endDate = ''
  }

  let configCascader = ref<{}>({
    corporateWidth: '250px', //公司宽度
    sectorWidth: '250px', //部门宽度
    corporateRquired: false, //公司是否必填
    sectorRquired: false, //部门是否必填
    size: 'small', //选择框大小'mini' | 'small' | 'medium' | 'large'
    corporateName: '公司', //公司label
    cascaderShow: 'sector',
    sectorName: '部门', //部门label
    corporateNameLabelWidth: '80px', //选择公司label宽
    sectorLabelWidth: '80px',
  })

  const changeCascader = (data: any) => {
    const { corporateId, sectorId, sectorLabel, corporateLabel } = data
    form.companyId = corporateId
    // form.firmName = corporateLabel
    // form.firmGroupId = sectorId
    // form.firmGroupName = sectorLabel
  }

  const ljlxList: any = ref([])

  const ljlxClick = () => {
    post({
      url: 'pieceData/ProduceSubsidyType/getList',
      data: {},
    }).then(({ data }) => {
      ljlxList.value = data
    })
  }

  const ljlxChange = (e: any) => {
    ljlxList.value.forEach((res: any) => {
      if (res.subsidyType == e) {
        nextTick(() => {
          dataForm.subsidyTypeAmount = res.subsidyAmount
        })
      }
    })
  }

  const dataListmx: any = ref([])
  const selectedKeys: any = ref([])

  let columnsTable: any = ref([
    {
      title: '员工工号',
      dataIndex: 'userCode',
    },
    {
      title: '员工姓名',
      dataIndex: 'userName',
    },
    {
      title: '岗位',
      dataIndex: 'postName',
    },
    {
      title: '织布单号',
      dataIndex: 'planOrderCode',
      slotName: 'planOrderCode',
    },
    {
      title: '机台号',
      dataIndex: 'machineCode',
      slotName: 'machineCode',
    },
    {
      title: '生产单补贴金额',
      dataIndex: 'subsidyAmount',
      slotName: 'subsidyAmount',
    },
    {
      title: '关联单号(备注)',
      dataIndex: 'remark',
      slotName: 'remark',
    },
  ])

  let tcAdds: any[] = columnsTable.value.map((o: any) => {
    let obj = {
      headerName: o.title,
      field: o.dataIndex,
      pinned: o['fixed'],
    }
    if (o.dataIndex === 'planOrderCode') {
      obj['resizable'] = false
      obj['filter'] = true
      obj['sortable'] = false
      obj['cellRenderer'] = {
        template: `
      <a-select popup-container="html" class="bacClass" allow-clear v-model="params.data.planOrderCode"
          @click="a" @change="changes" :disabled="pageData.sideTitle == '生产补贴单明细'" style="width: 170px" allow-search size="mini">
          <a-option v-for="item in planOrderCodelist" :value="item.planOrderCode">{{
            item.planOrderCode
          }}</a-option>
        </a-select>`,
        setup(props: any) {
          const a = () => {
            planOrderCodeClick()
          }
          const changes = (e) => {
            props.params.data.machineCode = ''
            setTimeout(() => {
              gridOptionAdds.value.setRowData(dataListmx.value)
            }, 0)
          }
          return {
            pageData,
            a,
            planOrderCodelist,
            changes,
          }
        },
      }
    }
    if (o.dataIndex === 'machineCode') {
      obj['resizable'] = false
      obj['filter'] = true
      obj['sortable'] = false
      obj['cellRenderer'] = {
        template: `
      <a-select popup-container="html" class="bacClass" allow-clear v-model="params.data.machineCode"
          @click="machineCodeClick(params.data.planOrderCode)" size="mini" style="width: 170px" allow-search :disabled="pageData.sideTitle == '生产补贴单明细'||!params.data.planOrderCode">
          <a-option v-for="item in machineCodeList" :value="item.machineCode">{{
            item.machineCode
          }}</a-option>
        </a-select>`,
        setup(props: any) {
          return {
            machineCodeList,
            pageData,
            machineCodeClick,
          }
        },
      }
    }
    if (o.dataIndex === 'subsidyAmount') {
      obj['resizable'] = false
      obj['filter'] = true
      obj['sortable'] = false
      obj['cellRenderer'] = {
        template: `<div>{{ params.data.subsidyAmount }}</div>`,
        setup(props: any) {
          props.params.data.subsidyAmount = dataForm.subsidyTypeAmount
          return {
            dataForm,
          }
        },
      }
    }
    if (o.dataIndex === 'remark') {
      obj['resizable'] = false
      obj['filter'] = true
      obj['sortable'] = false
      obj['cellRenderer'] = {
        template: ` <a-input class="bacClass" v-model="params.data.remark" :disabled="pageData.sideTitle == '生产补贴单明细'" />`,
        setup(props: any) {
          return {
            pageData,
          }
        },
      }
    }
    return obj
  })
  tcAdds = a.concat(tcAdds)

  let columnsTables: any = ref([
    {
      title: '员工工号',
      dataIndex: 'userCode',
    },
    {
      title: '员工姓名',
      dataIndex: 'userName',
    },
    {
      title: '岗位',
      dataIndex: 'postName',
    },
    {
      title: '织布单号',
      dataIndex: 'planOrderCode',
      slotName: 'planOrderCode',
    },
    {
      title: '机台号',
      dataIndex: 'machineCode',
      slotName: 'machineCode',
    },
    {
      title: '生产单补贴金额',
      dataIndex: 'subsidyAmount',
      slotName: 'subsidyAmount',
    },
    {
      title: '关联单号(备注)',
      dataIndex: 'remark',
      slotName: 'remark',
    },
    {
      title: '确认状态',
      dataIndex: 'confirmState',
      slotName: 'confirmState',
    },
  ])
  let tcAddsMx: any[] = columnsTables.value.map((o: any) => {
    let obj = {
      headerName: o.title,
      field: o.dataIndex,
      pinned: o['fixed'],
    }
    if (o.dataIndex === 'confirmState') {
      obj['resizable'] = false
      obj['filter'] = true
      obj['sortable'] = false
      obj['cellRenderer'] = {
        template: `
      <a-tag color="green" v-if="params.data.confirmState == 0">未确认</a-tag>
        <a-tag color="#45a769" v-else>已确认</a-tag>`,
        setup(props: any) {
          return {}
        },
      }
    }
    return obj
  })
  tcAddsMx = a.concat(tcAddsMx)
  let columnsTable2: any = ref([
    {
      title: '员工工号',
      dataIndex: 'code',
    },
    {
      title: '员工姓名',
      dataIndex: 'name',
    },
    // {
    //     title: '部门',
    //     dataIndex: 'departmentName',
    // },
    {
      title: '岗位',
      dataIndex: 'postName',
    },
  ])

  let tcAddYg: any[] = columnsTable2.value.map((o: any) => {
    let obj = {
      headerName: o.title,
      field: o.dataIndex,
      pinned: o['fixed'],
    }
    return obj
  })
  tcAddYg = a.concat(tcAddYg)
  const visible = ref(false)

  const handleOk = () => {
    if (gridOptionAddYg.value.getSelectedRows().length < 1) {
      Message.error('请勾选数据')
      return
    }
    for (let i = 0; i < gridOptionAddYg.value.getSelectedRows().length; i++) {
      let res = gridOptionAddYg.value.getSelectedRows().map((res) => res.id)[i]
      if (dataListmx.value.length >= 1) {
        if (dataListmx.value.map(({ userId }) => userId).includes(res)) {
          Message.error('请勿勾选相同员工')
          return
        }
      }
    }
    dataListmx2.value.forEach((res: any) => {
      if (
        gridOptionAddYg.value
          .getSelectedRows()
          .map((res) => res.id)
          .includes(res.id)
      ) {
        dataListmx.value.push({
          userId: res.id,
          userCode: res.code,
          userName: res.name,
          postId: res.postId,
          postName: res.postName,
        })
      }
    })
    gridOptionAdds.value.setRowData(dataListmx.value)
    visible.value = false
  }

  const frimIDandName = () => {
    post({
      url: 'admin/acl/user/findUserToken',
      data: {},
    }).then(({ data }) => {
      nextTick(() => {
        dataForm.companyId = data.data.firmId
        dataForm.companyName = data.data.firmName
      })
    })
  }

  const selectedKeys2: any = ref([])
  const dataListmx2: any = ref([])

  const xzClicks = () => {
    cascaderId.value = null
    // options.value = []
    dataListmx2.value = []
    selectedKeys2.value = []
    if (dataForm.departmentId) {
      visible.value = true
    } else {
      Message.error('请选择部门')
    }
  }

  const mxDelet = () => {
    if (gridOptionAdds.value.getSelectedRows().length < 1) {
      Message.error('请勾选要删除的数据')
      return
    }
    dataListmx.value = dataListmx.value.filter(function (o: any) {
      return (
        gridOptionAdds.value
          .getSelectedRows()
          .map((res) => res.userId)
          .indexOf(o['userId']) === -1
      )
    })
  }

  const options: any = ref()

  const cascaderId: any = ref()
  const cascaderName: any = ref()
  const cascaders: any = ref()

  const bmClick = () => {
    post({
      url: `org/baCompany/getDepartmentTree?companyId=${dataForm.companyId}`,
    }).then(({ data }) => {
      options.value = data
    })
  }

  watch(
    () => dataForm.departmentId,
    () => {
      nextTick(() => {
        dataForm.departmentName = slicing(cascaders?.value?.selectViewValue[0]?.label)
      })
    }
  )

  const mxSelect = () => {
    post({
        url: 'org/hrEmployee/getUserAndSecondment',
        data: {
          department_id: dataForm.departmentId,
          workTime: dayjs().format('YYYY-MM-DD'),
        },
      }).then(({ data }) => {
        dataListmx2.value = data
      })
  }

  const planOrderCodelist: any = ref([])
  const machineCodeList: any = ref([])

  const planOrderCodeClick = () => {
    post({
      url: 'planSingle/planOrder/getList',
      data: { neState: 0 },
    }).then(({ data }) => {
      planOrderCodelist.value = data
    })
  }
  const machineCodeClick = (e: any) => {
    post({
      url: 'planSingle/planMachineTask/getList',
      data: { planOrderCode: e },
    }).then(({ data }) => {
      machineCodeList.value = data
    })
  }
</script>
<style lang="less" scoped>
  :deep(.ag-cell) {
    border-right: 1px solid #dde2eb;
  }

  :deep(.ag-header-cell) {
    border-right: 1px solid #dde2eb;
  }

  :deep(.ag-cell-value) {
    user-select: initial;
    -moz-user-select: text;
    -ms-user-select: text;
    -webkit-user-select: text;
  }

  :deep(.arco-table-td-content) {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
</style>
